﻿using System;
using System.Web;
using System.Web.Mvc;

namespace System.Web.Mvc
{
    /// <summary>
    /// An attribute to help redirect a page when MvcTrialer's trialEnd date has expired
    /// </summary>
    public class TrialAttribute : ActionFilterAttribute
    {
        /// <summary>
        /// Check trial expiration during OnActionExecuting
        /// </summary>
        /// <param name="filterContext">ActionExecutingContext</param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (MvcTrialer.Trialer.trialOn && MvcTrialer.Trialer.IsTrialEnded())
            {
                filterContext.HttpContext.Response.Redirect(MvcTrialer.Trialer.redirectURL);
            }
        }
    }
}

namespace MvcTrialer
{
    /// <summary>
    /// Trialer Class
    /// </summary>
    public static class Trialer
    {
        /// <summary>
        /// Set the trial On/Off
        /// </summary>
        public static bool trialOn { get; set; }

        /// <summary>
        /// Trail end date (in UTC)
        /// </summary>
        public static DateTime trialEnd { get; set; }

        /// <summary>
        /// Redirect URL e.g. "Home/TrialEnd" or "http://mvctrialer.codeplex.com/"
        /// </summary>
        public static string redirectURL { get; set; }

        /// <summary>
        /// Check if a trialEnd date has expired
        /// </summary>
        /// <returns>True/False</returns>
        public static bool IsTrialEnded()
        {
            if (DateTime.UtcNow.Date > trialEnd.Date)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}